package kata.stringKata;

/**
 * @author cuiH
 * Date: 13-10-14
 */
public class Calculator {
    static int defaultNo = 0;

    public int add(String s) {
        if(s==null||s.equals("")) return defaultNo;
        s=format(s);
        return  addString(s);
    }

    private String format(String formatString) {
        return toSplitString(replaceString(formatString));
    }

    //做成标准的分割形式
    private String toSplitString(String s) {
        if(s.length()>2&&s.substring(0,2).equals("//")) {
            s=s.substring(s.indexOf("1"),s.length());
            s=s.replace(";",",").replace("%",",");
        }
        return s;
    }

    //处理String函数,
    private String replaceString(String s) {
        //处理/n
        s=s.replaceAll("\\n" ,",");
        return s;
    }

    //相加函数
    private int addString(String s) {
        String spiltString[] = s.split(",");
        int addResult = 0;
        for (String subString : spiltString) {
            addResult = addResult + toInteger(subString);
        }
        return addResult;
    }

    private int toInteger(String checkString) {
        if(checkString.equals("")||Integer.parseInt(checkString)>1000){
            return 0;
        } else{
            return Integer.parseInt(checkString);
        }
    }
}
